<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * Generic controller for entity class, CI AR.
 * @author hmQuan
 *
 */
class Chat extends MY_Controller {
	function __construct(){
		parent::__construct();
		$this->load->model('t_chat');
	}

	/**
	 * get chat log by id
	 * @param user id, gcm_id, page, number
	 * @return array|null
	 */
	public function get_chat_log(){
		// $post = $this->input->post();
		$post = parent::validation_user();

		// gcm id
		$gcm_regid = $post[GCM_REGID];
		// user id
		// if(!isset($post[CHAT_FROM]) || empty($post[CHAT_FROM])){
		// 	parent::print_result(array(CHAT_FROM => 'missing value'), 0);
		// 	exit();
		// }
			
		//$user_id to
		if(!isset($post[CHAT_TO]) || empty($post[CHAT_TO])){
			parent::print_result(array(CHAT_TO => 'missing value'), 0);
			exit();
		}
		if(isset($post['page'])){
			$page = $post['page'];
		}else{
			$page = 0;
		}

		if(isset($post['per_page'])){
			$per_page = $post['per_page'];
		}else{
			$per_page = 50;
		}
		$return = array();
		$chatLog = $this->t_chat->getChatLogSegment(array(CHAT_FROM => $post[T_USERS][USER_ID], CHAT_TO => $post[CHAT_TO]), $per_page, $page);
		if(!empty($chatLog)){
			$return[T_CHATS] = $chatLog;
			$this->load->model(T_USER);
			$userFrom = $this->t_user->get_data_by_id($post[T_USERS][USER_ID]);
			$return['st_FromUserName'] = $userFrom[0][USER_NAME];
			$userTo = $this->t_user->get_data_by_id($post[CHAT_TO]);
			$return['st_ToUserName'] = $userTo[0][USER_NAME];
			parent::print_result($return, SUCCESS_VALUE);
		}else{
			parent::print_result('No chat log');
		}
		
		exit();
	}

	public function get_chat_total_rows(){
		$post = $this->input->post();
		$post = parent::check_post_value($post);

		// gcm id
		$gcm_regid = $post[GCM_REGID];
		// user id
		if(isset($post[CHAT_FROM]) && !empty($post[CHAT_FROM])){
			$user_id_from = $post[CHAT_FROM];
		}else{
			parent::print_result(array(CHAT_FROM => 'missing value'), 0);
			exit();
		}
		//$user_id to
		if(isset($post[CHAT_TO]) && !empty($post[CHAT_TO])){
			$user_id_to = $post[CHAT_TO];
		}else{
			parent::print_result(array(CHAT_TO => 'missing value'), 0);
			exit();
		}
		$result = $this->t_chat->get_data_by_property(array(CHAT_FROM => $user_id_from, CHAT_TO => $user_id_to));

		parent::print_result(array(T_CHATS => count($result)), SUCCESS_VALUE);
		exit();
	}

	/**
	 * get chat log by id
	 * @param user id, gcm_id, page, number
	 * @return array|null
	 */
	public function get_notificate(){
		// $post = $this->input->post();
		$post = parent::validation_user();

		// gcm id
		$gcm_regid = $post[GCM_REGID];
		// user id
		// if(!isset($post[CHAT_FROM]) || empty($post[CHAT_FROM])){
		// 	parent::print_result(array(CHAT_FROM => 'missing value'), 0);
		// 	exit();
		// }
			
		//$user_id to
		if(!isset($post[CHAT_TO]) || empty($post[CHAT_TO])){
			parent::print_result(array(CHAT_TO => 'missing value'), 0);
			exit();
		}
		if(isset($post['page'])){
			$page = $post['page'];
		}else{
			$page = 0;
		}

		if(isset($post['per_page'])){
			$per_page = $post['per_page'];
		}else{
			$per_page = 50;
		}

		if(isset($post['max_id'])){
			$max_id = $post['max_id'];
		}else{
			$max_id = 0;
		}

		$return = array();
		$where = CHAT_TYPE.' != 3' .' AND '.
					CHAT_TYPE.'!=0'.' AND '.
					CHAT_TO .'='.$post[CHAT_TO];	
		
		if($max_id == 0 ){
			$chatLog = $this->t_chat->getChatLog('*' , $where, 50 , 0);
		}else{
			$where = $where.' AND '.CHAT_ID.'<'.$max_id;
			$chatLog = $this->t_chat->getChatLog('*' , $where, 50, 0 );
		}

		if(!empty($chatLog)){
			$return[T_CHATS] = $chatLog;
			$this->load->model(T_USER);
			$userFrom = $this->t_user->get_data_by_id($post[T_USERS][USER_ID]);
			$return['st_FromUserName'] = $userFrom[0][USER_NAME];
			$userTo = $this->t_user->get_data_by_id($post[CHAT_TO]);
			$return['st_ToUserName'] = $userTo[0][USER_NAME];
			parent::print_result($return, SUCCESS_VALUE);
		}else{
			parent::print_result('No notification');
		}
		
		exit();
	}



}